package com.example.controller;

import cn.hutool.core.lang.Dict;
import com.example.common.Result;
import com.example.controller.domain.SearchRequest;
import com.example.entity.Notebook;
import com.example.recommend.RecommendService;
import com.example.service.NotebookService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文艺交流笔记前端操作接口
 **/
@RestController
@RequestMapping("/notebook")
public class NotebookController {

    @Resource
    private NotebookService notebookService;
    @Resource
    private RecommendService recommendService;

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Notebook notebook) {
        notebookService.add(notebook);
        return Result.success("新增交流笔记成功");
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        notebookService.deleteById(id);
        return Result.success("删除交流笔记成功");
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        notebookService.deleteBatch(ids);
        return Result.success("删除交流笔记成功");
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody Notebook notebook) {
        notebookService.updateById(notebook);
        return Result.success("更新交流笔记信息成功");
    }

    /**
     * 根据ID查询
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Notebook notebook = notebookService.selectById(id);
        return Result.success("查询交流笔记信息成功",notebook);
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public Result selectAll(Notebook notebook) {
        List<Notebook> list = notebookService.selectAll(notebook);
        return Result.success("查询交流笔记信息列表成功",list);
    }

    /**
     * 分页查询
     */
    @GetMapping("/selectPage")
    public Result selectPage(Notebook notebook,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Notebook> page = notebookService.selectPage(notebook, pageNum, pageSize);
        return Result.success("分页查询交流笔记列表成功",page);
    }

    @PutMapping("/updateCount/{id}")
    public Result updateCount(@PathVariable Integer id) {
        notebookService.updateCount(id);
        return Result.success("更新浏览量成功");
    }

    /**
     * 我的收藏
     */
    @GetMapping("/selectCollect")
    public Result selectCollect(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "6") Integer pageSize) {
        return Result.success("查询我收藏的交流笔记成功",notebookService.selectCollect(pageNum,pageSize));
    }

    @GetMapping("/selectCount")
    public Result selectCount() {
        List<Dict> list = notebookService.selectCount();
        return Result.success("查询交流笔记统计数据成功",list);
    }

    @GetMapping("/listBySearchKeyword")
    public Result queryForListBySearchKeyword(SearchRequest searchRequest,
                                              @RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success("关键字搜索交流笔记成功",notebookService.queryForListBySearchKeyword(searchRequest, pageNum, pageSize));
    }

    @GetMapping("/listBySearchKeywordCount")
    public Result queryForListBySearchKeywordCount(SearchRequest searchRequest,
                                                   @RequestParam(defaultValue = "1") Integer pageNum,
                                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success("关键字搜索交流笔记成功",notebookService.queryForListBySearchKeywordCount(searchRequest, pageNum, pageSize));
    }
    @GetMapping("/queryForMyNotebook")
    public Result queryForMyNotebook(Notebook notebook, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "6") Integer pageSize){
        return Result.success("查询我发布的交流笔记成功",notebookService.queryForMyNotebook(notebook,pageNum,pageSize));
    }
    @GetMapping("/listRecommendNotebook")
    public Result queryForRecommendNotebookList(){
        return Result.success("推荐交流笔记成功",recommendService.recommendNotebook());
    }

}